Starting Out With Visual Basic (7th Edition) by Unknown

Starting Out With Visual Basic (7th Edition) by Unknown

Author:Unknown
Language: eng
Format: epub


The Interval property can be set to a value of 1 or greater. The value stored in the Interval

property is the number of milliseconds that elapse between timer events. A millisecond is a

thousandth of a second, so setting the Interval property to 1000 causes a timer event to occur

every second.

In Tutorial 8-7, you examine an application that demonstrates a Timer control.

Tutorial 8-7: The Timer Demo

1. Step 1: Open the Timer Demo project from the sample student programs folder named

Chap8\Timer Demo. The application’s form is shown in Figure 8-30. Notice that the

Timer control appears as a stopwatch in the component tray.

Figure 8-30 Timer Demo form

2. Step 2: Run the application. The form shown in Figure 8-31 appears.

Figure 8-31 Timer Demo

application running

3. Step 3: The number appearing under the Seconds Counter label is initially set to 0,

but it increments every second. After a few seconds, click the Stop Timer button to halt

the timer.

4. Step 4: When you click the Stop Timer button, the button’s text changes to Start

Timer. Click the button again to start the timer.

5. Step 5: After a few seconds, click the Exit button to end the application.

6. Step 6: With the Designer window open, select the Timer control.

7. Step 7: With the Timer control selected, look at the Properties window. The name of

the control is tmrSeconds. Its Enabled property is initially set to True, and its Interval

property is set to 1000.

8. Step 8: Open the Code window and notice that a class-level variable named

intSeconds is declared.

9. Step 9: Look at the tmrSeconds_Tick event handler. The code is as follows:

Private Sub tmrSeconds_Tick(...) Handles tmrSeconds.Tick

' Update the seconds display by one second.

intSeconds += 1

lblCounter.Text = intSeconds.ToString()

End Sub

Each time the tmrSeconds_Tick event handler executes, it adds 1 to intSeconds and

then copies its value to the lblCounter label. Because the Timer control’s Interval

property is set to 1000, this event handler executes every second (unless the Timer

control’s Enabled property equals False).

10. Step 10: The button that stops and starts the timer is named btnToggleTimer. Look at

the btnToggleTimer_Click event handler. The code is as follows:

Private Sub btnToggleTimer_Click(...) Handles btnToggleTimer.Click

' Toggle the timer.

If tmrSeconds.Enabled = True Then

tmrSeconds.Enabled = False

btnToggleTimer.Text = "&Start Timer"

Else

tmrSeconds.Enabled = True

btnToggleTimer.Text = "&Stop Timer"

End If

End Sub

If tmrSeconds.Enabled equals True, the code sets it to False and changes the button’s

text to & Start Timer. Otherwise, it sets the property to True and changes the button’s

text to & Stop Timer.

In Tutorial 8-8 you will use a Timer control to create a game application.

Tutorial 8-8: Creating the Catch Me game



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.